//
//  CTUserFavoriteList.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/25.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTUserFavoriteList.h"
#import "CTUserFavoriteCell.h"
#import "CTHeader.h"
#import "CTDeviceFunction.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"

@interface CTUserFavoriteList()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView         *favTableView;
}

@end

@implementation CTUserFavoriteList

@synthesize favDataArray,delegate;

- (void)dealloc
{
    [favTableView removeFromSuperview];
    favTableView = nil;
    delegate = nil;
    favDataArray = nil;
    [CTDebugManager showErrorLogInfo:@"dealloc : CTUserFavoriteList Is Removed!!!!"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self createUserFavoriteListTableView];
        
    }
    return self;
}
- (void)createUserFavoriteListTableView
{
    favTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 0) style:UITableViewStylePlain];
    favTableView.delegate = self;
    favTableView.dataSource = self;
    favTableView.showsVerticalScrollIndicator = NO;
    favTableView.backgroundColor = [UIColor redColor];
    favTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    favTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    favTableView.backgroundColor = [UIColor whiteColor];
    favTableView.rowHeight = 105.0f;
    [favTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:favTableView];
     favDataArray= [NSMutableArray arrayWithCapacity:1];
}




#pragma mark - Table view data source

#pragma mark-UITableViewDataSource
//分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    return @">>>";
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 37)];
    myView.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:240/255.0 alpha:1.0];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, CT_HEADER_SCREEN_WIDTH, 37)];
    titleLabel.textColor=[UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:12.0f];
    //
    titleLabel.text=@"常用表情";
    [myView addSubview:titleLabel];
    return myView;
}

//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    CTUserFavoriteCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[CTUserFavoriteCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return favDataArray.count;
}

#pragma mark-UITableViewRemoveCell
//设置编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//    [super setEditing:editing animated:animated];
    
    [favTableView setEditing:editing animated:animated];
}

//1是否编辑 DataSource  可以滑动删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//delegate  //2指定行编辑
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
    
}
//设置多个删除按
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        //更新数据
//        [favDataArray removeObjectAtIndex:indexPath.row];
        
        // 2. 更新UI
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
//        if(editingStyle == UITableViewCellEditingStyleDelete)
//        {
//            NSString *sectionName = [_keyArray objectAtIndex:indexPath.section];
//            NSMutableArray *sectionArray = [_listData objectForKey:sectionName];
//            [sectionArray removeObjectAtIndex:indexPath.row];//删除数据源
//            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//再删除UI行
//            
//            if (sectionArray.count == 0) {
//                
//                [_keyArray removeObjectAtIndex:indexPath.section];
//                [_listData removeObjectForKey:sectionName];
//                
//                [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];//删除sections
//                [tableView reloadData];
//            }
//            
//        }

    }];
    deleteRowAction.backgroundColor = CT_HEADER_ORANGE_COLOR;
    UITableViewRowAction *setRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"设为\n常用" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        // 1. 更新数据
        [favDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        
        // 2. 更新UI
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    }];
    setRowAction.backgroundColor = [UIColor colorWithRed:111/255.0 green:197/255.0 blue:36/255.0 alpha:1.0];
    return @[deleteRowAction,setRowAction];
}
//3编辑样式(完成编辑)   编辑时先操作数据源 再改 UI
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if(editingStyle == UITableViewCellEditingStyleDelete)
//    {
//        
//        if (_dataArray.count != 0) {
//            [_dataArray removeObjectAtIndex:indexPath.row];
//            [self reloadPushData];
//            //判断当前通知页面是否有显示“暂无通知图片”
//            [self isShowNotiMessageOrNot];
//        }
//        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//        [tableView reloadData];
//    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
}

#pragma  mark -setter-
- (void)setFavDataArray:(NSMutableArray *)ary
{
    favDataArray =[NSMutableArray arrayWithArray:ary];
    favTableView.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, favDataArray.count*105);
}
@end
